/*
 * @Author: cgl
 * @Date: 2023-07-07 13:50:47
 * @LastEditors: cgl
 * @LastEditTime: 2023-07-17 13:56:54
 * @FilePath: \public-health-emergencies-fed:\radiation-hygiene-fe\src\views\slide\components\util.js
 * @Description:
 *
 * Copyright (c) 2023 by cgl/微盛信息产业有限公司, All Rights Reserved.
 */

/**
 * 录入格式校验
 * @param {*} node 校验的当前节点
 */
let black = ['el-input--suffix'];
import JSONbig from "json-bigint"
export function validate(node, _this) {
	let elSelect = elHaveComponent(node)
	// console.dir(node);
	if(node._prevClass.includes("el-input__inner")) {
		if (black.some(item => node.parentNode._prevClass.includes(item))) return;
		if(node.dataset.cusvalidate == "text") return;
		//
		let isTrue, message;

		if (node.dataset.cusvalidate == "integer" && !isNumber(node.value)) {
			isTrue = true
			message = "请输入整数值";
		} else if(node.dataset.cusvalidate == "text") {
			isTrue = false
		} else {
			isTrue = isNaN(node.value)
			message = '请输入正确的格式'
		}


		if(isTrue) {
			node.parentNode.__vue__.handleInput({
				target: {value: ''}
			});
			node.parentNode.__vue__.focus();
			// node.style.borderColor = "#ff3d3d";
			setTimeout(() => {
				elSelect.$el.parentNode.classList.add("error");
			}, 0)
			_this.$modal.msgWarning(message);
		}else if(node.style) {
			elSelect.$el.parentNode.style.border = "";
		}
	}
}

/**
 * input    el
 * output   带__vue__
 * @param {*} el 组件子节点
 * @param {*} rootCompent 组件顶层节点
 * @param {*} num 防止向上寻找套娃获取vueComponent
 * @returns
 */
export function elHaveComponent(el, rootCompent, num = 0) {
	// if (num == 0) {validate(el)}
	if (!el || num > 2) return rootCompent || el
	if (el.parentNode && el?.__vue__) {
		return elHaveComponent(el.parentNode, el.__vue__, num + 1)
	}
	return elHaveComponent(el.parentNode, (el?.__vue__ || rootCompent), num + 1)
}

/**
 * input
 * output
 * @param {*} el 组件子节点
 * @param {*} callback 绑定的更改请求回调
 * @param {*} type 绑定类型 1：不绑定高亮显示
 * @returns
 */
export function bind(el, callback, type) {
	let elSelect = elHaveComponent(el)
	// 值改变校验并请求
	elSelect.$on('change', function(){
		try {
			validate(el, this);
		} catch (error) {
			console.log(error)
		}
		callback();
	})
	// 输入框聚焦td高亮
	if(type != 1) {
		elSelect.$on('focus', function(){
			if(elSelect.$el.parentNode.nodeName == "TD") {
				elSelect.$el.parentNode.classList.add("heighlight");
			}
		})
		elSelect.$on('blur', function(){
			elSelect.$el.parentNode.classList.remove("heighlight", "error");
		})
	}
	// 单选框双击取消选中
	if(el._prevClass.includes("el-radio__original")) {
		el.onchange = (e) => {
			el.ischange = true;
		}
		if (el.parentNode.parentNode.dataset?.must != '1') {
			el.onclick = (e) => {
				setTimeout(() => {
					if(!el.ischange) {
						elSelect.model = "";
						elSelect.handleChange();
					}
				}, 0)
				el.ischange = false;
			}
		}
	}
	// 输入框移入自动全选
	el.onmouseover = (e) => {
		if (elSelect.select) {
			try {
				elSelect.select();
			} catch (error) {
				// const findEl = elSelect.$children.find(childEl => childEl?.$el?.className.includes('el-input') && childEl?.$el?.className.includes('el-input--medium'))
				// if (findEl) findEl.select();
			}
		}
	}
}
export function unbind(el, callback, type) {
	let elSelect = elHaveComponent(el)
	// 值改变校验并请求
	elSelect.$off('change');
}

export function isNumber(num) {
	return Number(num) % 1 === 0
}

export function jsonParse(json){
    let ret = JSONbig.parse(json)
    return changeBigInt(ret)
}
function changeBigInt(ret){
  if(ret instanceof Array){
    ret.forEach(item => {
       changeBigInt(item)
    })
  }else{
    if(typeof ret === 'object'){
      for(let key in ret){
        if(key == 's'){
          ret = ret.toString()
          break
        }
        ret[key] = changeBigInt(ret[key])
      }
    }
    return ret
  }
  return ret
}

export default {
	validate,
	elHaveComponent,
	bind,
  unbind,
  jsonParse
}
